// pages/car-list/index.js
import dayjs from "dayjs";
import {
    getDay
} from '../../utils/date'
import store from '../../utils/store'
import {typeEnum} from "../../enum/laneStatus";
import router from "../../utils/router";
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        licenseNo: '', // 车牌
        typeEnum: typeEnum,
        datalist: [],
        dataILu: true, //是否还有更多数据
        pageIndex: 0,
        pageSize: 10,
        show: false,
        minDate: new Date(2020, 0, 1).getTime(),
        maxDate: new Date().getTime(),
        startTime: getDay(-7),
        endTime: getDay(0),
        today: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options && options.today === 'today') {
            this.setData({
                today: true,
                startTime: getDay(0),
                endTime: getDay(0)
            })
            this.getData()
        } else {
            this.getData()
        }
    },

    handleInputChange(e) {
        console.log('e', e)
        const value = e.detail.value
        this.setData({
            licenseNo: value
        })
    },

    handleClickTime() {
        this.setData({
            show: true
        })
    },

    handleSearch() {
        this.setData({
            datalist: [],
            dataILu: true,
            pageIndex: 0,
            pageSize: 10
        })
        this.getData()
    },

    onClose() {
        this.setData({
            show: false
        })
    },
    onConfirm(event) {
        let [start, end] = event.detail;
        start = dayjs(start).format('YYYY-MM-DD')
        end = dayjs(end).format('YYYY-MM-DD')
        this.setData({
            startTime: start,
            endTime: end,
            show: false
        })
    },

    handleClickDetail(e) {
        const itemDetail = e.currentTarget.dataset.item
        store.setItem('currentCarDetail', itemDetail)
        router.push('cardetail')
    },



    getData() {
        //如果没有更多数据了就退出
        if (!this.data.dataILu) return
        this.data.pageIndex++
        const factoryId = store.getItem("factoryId")
        app.clientPost(`${app.ClientApi.getAllCarList}/page-num/${this.data.pageIndex}/page-size/${this.data.pageSize}/factoryId/${factoryId}`, {
            startTime: this.data.startTime + ' 00:00:00',
            endTime: this.data.endTime + ' 23:59:59',
            licenseNo: this.data.licenseNo
        }).then((res) => {
            console.log('res', res)
            const newList = [...this.data.datalist, ...res.list]
            const total = res.total
            this.setData({
                datalist: newList,
                dataILu: newList.length < total
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getData() //获取数据
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})